package com.yunda.AJDeviceService.controller;

import com.alibaba.fastjson.JSON;
import com.dahuatech.hutool.crypto.digest.DigestUtil;
import com.yunda.AJDeviceService.common.web.domain.AjaxResult;
import com.yunda.AJDeviceService.common.web.domain.KMSAjaxResult;
import com.yunda.AJDeviceService.utils.DataUtils;
import com.yunda.AJDeviceService.utils.DateUtils;
import com.yunda.AJDeviceService.utils.IdUtils;
import com.yunda.AJDeviceService.utils.PictureUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.UUID;

/**
 * @author lq
 * @className ErLangsKMSController
 * @description TODO
 * @date 2023/2/13 15:49
 */
@RestController
@Slf4j
@RequestMapping
public class ErLangsKMSController {

    private static String osName = System.getProperty("os.name").toLowerCase();

    @Value("${UploadFile.windowsimagespath}")
    private String windowsimagespath;

    @Value("${UploadFile.linuximagespath}")
    private String linuximagespath;

    @Value("${dbctCode}")
    private String fbCode;

    /**
     * KMS HTTP上传文件
     * @param imageFile
     * @return
     */
    @RequestMapping(value="/kms/services/rest/uploadService/httpFileUpload",method = RequestMethod.POST)
    public KMSAjaxResult httpFileUpload(@RequestParam("file") MultipartFile imageFile) {
        // /u02/data/inteCheckImg/
        log.info(" *** 二郎神安检机上传文件 ***");

        String scanDate = DateUtils.getDate();
        String id = IdUtils.fastSimpleUUID();
        String thumbId = IdUtils.fastSimpleUUID();

        KMSAjaxResult kmsAjaxResult = new KMSAjaxResult();

        FileInputStream in = null;
        try {
            String imageName=imageFile.getOriginalFilename();
            String mkdirStr="";
            if(osName.contains("windows")){
                mkdirStr=windowsimagespath.concat(scanDate).concat("\\").concat(fbCode).concat("\\").concat("\\").concat(id).concat("\\");
            }else {
                mkdirStr=linuximagespath+ scanDate +"/"+ fbCode+"/"+ id+"/";
            }
            in = (FileInputStream) imageFile.getInputStream();
            BufferedImage image = ImageIO.read(in);
            PictureUtils.createImage(image, mkdirStr, imageName);
            String url = "/inteCheckImg/"+ scanDate +"/"+ fbCode+"/"+ id+"/"+imageName+"?id="+thumbId;

            File file = new File(mkdirStr+"/"+imageName);
            String docMd5 = DataUtils.getMD5(file);

            kmsAjaxResult = KMSAjaxResult.success();
            kmsAjaxResult.put("id",id);
            kmsAjaxResult.put("docMd5",docMd5);
            kmsAjaxResult.put("url",url);
            kmsAjaxResult.put("thumbUrl","");
            kmsAjaxResult.put("beginTime",System.currentTimeMillis());
            kmsAjaxResult.put("endTime",System.currentTimeMillis());
            log.info(" *** 二郎神安检机上传文件 成功 :{}***",JSON.toJSON(kmsAjaxResult));
            return kmsAjaxResult;
        } catch (Exception e) {
            return KMSAjaxResult.error("二郎神安检机上传文件上传失败"+e.getMessage());
        }finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
